# 1、导包
import time

from selenium import webdriver
from time import sleep

from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service

# 2、实例化浏览器对象
driver_path = "C:/Users/Lenovo/AppData/Local/Programs/Python/Python310/geckodriver.exe"
service = Service(executable_path=driver_path)
driver = webdriver.Firefox(service=service)

# TODO frame框架
# 3、需求：打开frame页面
'''
driver.get("file:///E:/project/learn-python/html%E5%9F%BA%E7%A1%80/c-frame.html")
# ① 填写注册A的注册信息
driver.switch_to.frame('frame')
driver.find_element(By.XPATH, '//form//*[@id="username"]').send_keys('admin-main')
driver.find_element(By.NAME, 'password').send_keys('123456')

# ② 填写注册B的注册信息
driver.switch_to.default_content()
driver.switch_to.frame('frame1')
driver.find_element(By.ID, 'username_1').send_keys('admin-1')
driver.find_element(By.CSS_SELECTOR, 'form [placeholder="请输入密码"]').send_keys('123456')

# ③ 填写注册C的注册信息
driver.switch_to.default_content()
driver.switch_to.frame('frame2')
driver.find_element(By.ID, 'username_2').send_keys('admin-2')
driver.find_element(By.NAME, 'password_2').send_keys('123456')
'''

# TODO 窗口切换
# 3、需求：打开注册页面
driver.get('file:///E:/project/learn-python/html%E5%9F%BA%E7%A1%80/c-frame.html')
# ① 点击注册A页面链接
# a. 切换frame框架
driver.switch_to.frame('frame')
driver.find_element(By.XPATH, '//form//*[@id="zc1"]').click()
# print('所有窗口的句柄值：', driver.window_handles)
# b. 切换最新窗口
handles = driver.window_handles
driver.switch_to.window(handles[-1])

# ② 在打开的页面中，填写注册信息
driver.find_element(By.ID, 'username_1').send_keys("admin-1")
driver.find_element(By.ID, 'password_1').send_keys("123456")

# 关闭注册A页面
time.sleep(2)
driver.close()

# !! 注意：还想操作原始页面，需先切换窗口
driver.switch_to.window(handles[0])
driver.switch_to.frame('frame')

# !! 在打开的页面中，填写注册信息
driver.find_element(By.ID, 'username').send_keys("admin-main")
driver.find_element(By.ID, 'password').send_keys("123456")

# 退出浏览器
time.sleep(2)
driver.quit()